<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="80px">
                <!-- <el-form-item label="日期" prop="date">
                    <el-date-picker v-model="queryParams.date" placeholder="请选择日期"></el-date-picker>
                </el-form-item> -->
                <el-form-item label="时间" prop="customDate">
                    <el-radio-group v-model="queryParams.customDate" @change="changeCustomDate">
                        <el-radio-button :label="1">今天</el-radio-button>
                        <el-radio-button :label="2">昨天</el-radio-button>
                        <el-radio-button :label="3">近7天</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="日期" prop="date">
                    <el-date-picker v-model="queryParams.date" format="yyyy-MM-dd" type="daterange" placeholder="请选择日期" style="width: 240px"></el-date-picker>
                </el-form-item>
                <!-- <el-form-item label="SPU" prop="spu">
                    <el-input v-model.trim="queryParams.spu" placeholder="请输SPU" clearable style="width: 180px"/>
                </el-form-item> -->
                <!-- <el-form-item label="开发类型" prop="develop_type">
                    <el-select v-model="queryParams.develop_type" placeholder="请选择开发类型" clearable  style="width: 160px">
                        <el-option v-for="(dict, index) in dict.developTypeList" :key="index" :label="dict.label" :value="dict.value"></el-option>
                    </el-select>
                </el-form-item> -->
                
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <!-- <el-button type="primary" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button> -->
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script>
                <!-- <el-table-column label="日期" prop="date_msg"></el-table-column> -->
                <!-- <el-table-column label="SPU" prop="spu">
                    <template slot-scope="scope">
                        <el-link type="primary" @click="showSpuInfoDialog(scope.row)">{{ scope.row.spu }}</el-link>
                    </template>
                </el-table-column> -->
                <!-- <el-table-column label="产品图片">
                    <template slot-scope="scope">
                        <ImageUploadImg v-model="scope.row.product_images" :readOnly="true"></ImageUploadImg>
                    </template>
                </el-table-column> -->
                <el-table-column label="支付工具" prop="tag_campaign"></el-table-column>
                <el-table-column label="金额$"  prop="total_price"></el-table-column>
                <el-table-column label="占比"  prop="percentage"></el-table-column>
                <!-- <el-table-column label="操作" width="160">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-search" @click="showRankDialog(scope.row)">spu销量排行</el-button>
                    </template>
                </el-table-column> -->
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        <sku-info-dialog v-model="infoDialog.visible" v-if="infoDialog.visible" :params="infoDialog.data" @success="handleQuery"></sku-info-dialog>
        <SpuSalesRankingDialog v-model="rankDialog.visible" v-if="rankDialog.visible" :params="rankDialog.data"></SpuSalesRankingDialog>
    </div>
</template>

<script>
import { getDevelopType } from '@/api/product'
import { queryOrderReportSku, exportOrderReportSku,queryPaymentTools } from '@/api/report'
import { exportFile } from '@/utils/index'
import SkuInfoDialog from '../components/SkuInfoDialog.vue'
import SpuSalesRankingDialog from '../components/SpuSalesRankingDialog.vue'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'SKUReportList',
    components: { SkuInfoDialog, SpuSalesRankingDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                shop_id: undefined,
                customDate: 2,
                develop_type: undefined,
                date: [],
            },
            infoDialog: {
                visible: false,
                data: {}
            },
            rankDialog: {
                visible: false,
                data: {}
            },
            dict: {
                developTypeList: [] 
            }
        }
    },
    watch: {
        'queryParams.date': {
            handler(val) {
                if (val?.length == 2) {
                    this.queryParams.customDate = 0
                }
            },
            immediate: true,
            deep: true,
        },
    },
   async created() {
        try {
            const developTypeData = await getDevelopType()
            this.dict.developTypeList = developTypeData?.data ?? []
        } catch (error) {
            this.loading = false
        }
        this.getList()
    },
    methods: {
        changeCustomDate() {
            this.queryParams.date = []
            this.handleQuery()
        },
        showRankDialog(row) {
            this.rankDialog.visible = true
            this.rankDialog.data.spu = row.spu
            this.rankDialog.data.customDate = this.queryParams.customDate
            this.rankDialog.data.date = this.queryParams.date
        },
        showSpuInfoDialog(row) {
            this.infoDialog.visible = true
            this.infoDialog.data = row
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if(qyparams.date.length==2) {
                qyparams.date = [parseTime(qyparams.date[0], 'y-m-d'), parseTime(qyparams.date[1], 'y-m-d')]; 
            }
            exportOrderReportSku(qyparams)
                .then(res => {
                    console.log(res)
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    console.log(err)
                    this.download = false
                })
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if(qyparams.date.length==2) {
                qyparams.date = [parseTime(qyparams.date[0], 'y-m-d'), parseTime(qyparams.date[1], 'y-m-d')]; 
            }
            queryPaymentTools(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>